home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / answers / objects.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.9 KB  |  251 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* objects.c - open a window and clear the background.
  19.  *    Set up callbacks to handle keyboard input.
  20.  *      Model some objects.
  21.  *
  22.  *    F1 key            - print help information
  23.  *    SPACE key        - generates a random background color
  24.  *    <s> key            - toggle smooth/flat shading
  25.  *    Escape Key        - exit program
  26.  */
  27. #include <GL/gl.h>
  28. #include <GL/glu.h>
  29. #include <GL/glut.h>
  30.  
  31. #include <stdio.h>
  32. #include <math.h>
  33.  
  34. /* Function Prototypes */
  35.  
  36. GLvoid initgfx( GLvoid );
  37. GLvoid keyboard( GLubyte, GLint, GLint );
  38. GLvoid specialkeys( GLint, GLint, GLint );
  39. GLvoid drawScene( GLvoid );
  40.  
  41. void checkError( char * );
  42. void printHelp( char * );
  43.  
  44. /* Global Variables */
  45.  
  46. static char *progname; 
  47.  
  48. /* Global Definitions */
  49.  
  50. #define KEY_ESC    27    /* ascii value for the escape key */
  51.  
  52. void
  53. main( int argc, char *argv[] )
  54. {
  55.     GLsizei width, height;
  56.  
  57.     glutInit( &argc, argv );
  58.  
  59.     /* create a window that is 1/4 the size of the screen,
  60.      * and position it in the middle of the screen.
  61.      */
  62.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  63.     height = glutGet( GLUT_SCREEN_HEIGHT );
  64.     glutInitWindowPosition( width / 4, height / 4 );
  65.     glutInitWindowSize( width / 2, height / 2 );
  66.     glutInitDisplayMode( GLUT_RGBA );
  67.     glutCreateWindow( argv[0] );
  68.  
  69.     initgfx();
  70.  
  71.     glutKeyboardFunc( keyboard );
  72.     glutSpecialFunc( specialkeys );
  73.     glutDisplayFunc( drawScene ); 
  74.  
  75.     progname = argv[0];
  76.  
  77.     printHelp( progname );
  78.  
  79.     glutMainLoop();
  80. }
  81.  
  82. void
  83. printHelp( char *progname )
  84. {
  85.     fprintf(stdout, 
  86.         "\n%s - model some objects\n\n"
  87.         "F1 key        - print help information\n"
  88.         "SPACE Key    - generates a random background color\n"
  89.         "<s> key        - toggle smooth/flat shading\n"
  90.         "Escape Key    - exit the program\n\n",
  91.         progname);
  92. }
  93.  
  94. GLvoid
  95. initgfx( GLvoid )
  96. {
  97.     /* set clear color to blue */
  98.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  99.  
  100.     glOrtho( -2.0, 2.0, -2.0, 2.0, -1.0, 1.0 );
  101. }
  102.  
  103. void 
  104. checkError( char *label )
  105. {
  106.     GLenum error;
  107.     while ( (error = glGetError()) != GL_NO_ERROR )
  108.         printf( "%s: %s\n", label, gluErrorString(error) );
  109. }
  110.  
  111. GLvoid 
  112. keyboard( GLubyte key, GLint x, GLint y )
  113. {
  114.     static GLboolean flat = GL_FALSE;
  115.  
  116.     switch (key) {
  117.     case ' ':    /* SPACE key */
  118.         /* generate a random background color */
  119.         glClearColor( drand48(), drand48(), drand48(), 1.0 ); 
  120.         glutPostRedisplay();
  121.         break;
  122.     case 's':    /* s key */
  123.         /* toggle between smooth and flat shading */
  124.         flat = !flat;
  125.         if (flat)
  126.             glShadeModel( GL_FLAT );
  127.         else 
  128.             glShadeModel( GL_SMOOTH );
  129.         glutPostRedisplay();
  130.         break;
  131.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  132.         exit(0);
  133.     }
  134. }
  135.  
  136. GLvoid 
  137. specialkeys( GLint key, GLint x, GLint y )
  138. {
  139.     switch (key) {
  140.     case GLUT_KEY_F1:    /* Function key #1 */
  141.         /* print help information */
  142.         printHelp( progname );
  143.         break;
  144.     }
  145. }
  146.  
  147. GLvoid
  148. drawScene( GLvoid )
  149. {
  150.     static GLfloat red[] = { 1.0, 0.0, 0.0 };
  151.     static GLfloat yellow[] = { 1.0, 1.0, 0.0 };
  152.     static GLfloat blue[] = { 0.0, 0.0, 1.0 };
  153.     static GLfloat green[] = { 0.0, 1.0, 0.0 };
  154.     static GLfloat darkgreen[] = { 0.0, 0.25, 0.0 };
  155.  
  156.     /* left window */
  157.     static GLfloat v0[] = { -0.4, 0.8 };
  158.     static GLfloat v1[] = { -0.4, 0.4 };
  159.     static GLfloat v2[] = { -0.3, 0.8 };
  160.     static GLfloat v3[] = { -0.3, 0.4 };
  161.     static GLfloat v4[] = { -0.2, 0.8 };
  162.     static GLfloat v5[] = { -0.2, 0.4 };
  163.  
  164.     /* right window */
  165.     static GLfloat v6[] = { 0.2, 0.8 };
  166.     static GLfloat v7[] = { 0.2, 0.4 };
  167.     static GLfloat v8[] = { 0.3, 0.8 };
  168.     static GLfloat v9[] = { 0.3, 0.4 };
  169.     static GLfloat v10[] = { 0.4, 0.8 };
  170.     static GLfloat v11[] = { 0.4, 0.4 };
  171.  
  172.     glClear( GL_COLOR_BUFFER_BIT );
  173.  
  174.     /* draw the ground in different shades of green */
  175.     glBegin( GL_POLYGON );
  176.         glColor3fv( green );
  177.         glVertex2f( -2.0, -2.0 );
  178.         glVertex2f( 2.0, -2.0 );
  179.         glColor3fv( darkgreen );
  180.         glVertex2f( 2.0, 0.0 );
  181.         glVertex2f( -2.0, 0.0 );
  182.     glEnd();
  183.  
  184.     /* draw the house */
  185.     glColor3f( 1.0, 1.0, 1.0 ); /* white */
  186.     glRectf( -0.5, -0.5, 0.5, 1.0 );
  187.  
  188.     /* draw the door */
  189.     glColor3f( 0.5, 0.2, 0.1 ); /* brown */
  190.     glRectf( -0.2, -0.5, 0.2, 0.2 );
  191.  
  192.     /* draw the roof */
  193.     glBegin( GL_TRIANGLES );
  194.         glColor3f( 0.0, 0.0, 0.0 ); /* black */
  195.         glVertex2f( -0.5, 1.0 );
  196.         glVertex2f( 0.5, 1.0 );
  197.         glVertex2f( 0.0, 1.5 );
  198.     glEnd();
  199.  
  200.     /* draw 2 quadrilateral strip to make a window */
  201.     glBegin( GL_QUAD_STRIP );
  202.         glColor3f( 1.0, 0.0, 0.0 );
  203.         glVertex2fv (v0);
  204.         glColor3f( 0.9, 0.0, 1.0 );
  205.         glVertex2fv (v1);
  206.         glColor3f( 0.8, 0.1, 0.0 );
  207.         glVertex2fv (v2);
  208.         glColor3f( 0.7, 0.2, 1.0 );
  209.         glVertex2fv (v3);
  210.         glColor3f( 0.6, 0.3, 0.0 );
  211.         glVertex2fv (v4);
  212.         glColor3f( 0.5, 0.4, 1.0 );
  213.         glVertex2fv (v5);
  214.         glColor3f( 0.4, 0.5, 0.0 );
  215.     glEnd();
  216.  
  217.     /* draw 2 quadrilateral strip to make a window */
  218.     glBegin( GL_QUAD_STRIP );
  219.         glVertex2fv (v6);
  220.         glColor3f( 0.3, 0.6, 1.0 );
  221.         glVertex2fv (v7);
  222.         glColor3f( 0.2, 0.7, 0.0 );
  223.         glVertex2fv (v8);
  224.         glColor3f( 0.1, 0.8, 1.0 );
  225.         glVertex2fv (v9);
  226.         glColor3f( 0.0, 0.9, 0.0 );
  227.         glVertex2fv (v10);
  228.         glColor3f( 0.0, 1.0, 1.0 );
  229.         glVertex2fv (v11);
  230.     glEnd();
  231.  
  232.     /* Draw a triangle fan for the sun; make
  233.      * the center white, and the edges yellow
  234.      */
  235.     glBegin( GL_TRIANGLE_FAN );
  236.       glColor3f( 1.0, 1.0, 1.0 );
  237.       glVertex2f( 1.5, 1.5 );
  238.       glColor3fv( yellow );
  239.       glVertex2f( 1.5, 1.3 );
  240.       glVertex2f( 1.7, 1.4 );
  241.       glVertex2f( 1.7, 1.6 );
  242.       glVertex2f( 1.5, 1.7 );
  243.       glVertex2f( 1.3, 1.6 );
  244.       glVertex2f( 1.3, 1.4 );
  245.       glVertex2f( 1.5, 1.3 );
  246.     glEnd();
  247.  
  248.     checkError( "drawScene" );
  249.     glFlush();
  250. }
  251.